/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Desarrollo.Soft.Hibernate.ViewPrincipal.views;

import Desarrollo.Soft.Hibernate.Control.Consultas.ctrlAlmacen;
import Desarrollo.Soft.Hibernate.Control.ctrlGeneric;
import Desarrollo.Soft.Hibernate.Data.Entity.Catproductos;
import Desarrollo.Soft.Hibernate.Data.Entity.Tblalmacen;
import Desarrollo.Soft.Hibernate.Data.Entity.Tblcomprasproductos;
import Desarrollo.Soft.Hibernate.Data.Entity.Tblproductosproveedores;
import Desarrollo.Soft.Hibernate.Data.Entity.Tblproveedores;
import static java.awt.image.ImageObserver.WIDTH;
import java.util.List;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ASPIRE
 */
public class intFrmAlmacen extends javax.swing.JInternalFrame {

    DefaultTableModel modelo = new DefaultTableModel();
int idA=0;
    /**
     * Creates new form intFrmAlmacen
     */
    public intFrmAlmacen() {
        initComponents();
        Object[] columna = new Object[8];
        modelo.addColumn("Id");
        modelo.addColumn("Cantidad");
        modelo.addColumn("Producto");
        modelo.addColumn("Precio unitario");
        modelo.addColumn("Precio de Mayoreo");
        modelo.addColumn("Stock Maximo");
        modelo.addColumn("Stock minimo");
        modelo.addColumn("Unidad de medida");

        for (Tblalmacen almacen : ctrlGeneric.<Tblalmacen>selectItems(Tblalmacen.class)) {
            columna[0] = almacen.getId();
            columna[1] = almacen.getCantidad();
            columna[2] = almacen.getCatproductos().getDescripcion();
            columna[3] = almacen.getPrecioUnitario();
            columna[4] = almacen.getPreciomayoreo();
            columna[5] = almacen.getStockMaximo();
            columna[6] = almacen.getStockminimo();
            columna[7] = almacen.getUnidadmedida();
            modelo.addRow(columna);
        }
        jtableAll.setModel(modelo);
        this.cargarComboProvedores();

    }

    public final void cargarComboProvedores() {
        try {
            List<Tblproveedores> listaProveedores = ctrlGeneric.<Tblproveedores>selectItems(Tblproveedores.class);
            cmbProvedores.removeAllItems();
            for (int i = 0; i < listaProveedores.size(); i++) {
                cmbProvedores.addItem(new Item(listaProveedores.get(i).getId(), listaProveedores.get(i).getNombeproveedor()));
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane3 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtableAll = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        txtPrecioUnitario = new javax.swing.JTextField();
        txtStockMinimo = new javax.swing.JTextField();
        txtPrecioMayoreo = new javax.swing.JTextField();
        txtStockMaximo = new javax.swing.JTextField();
        txtCantidad = new javax.swing.JTextField();
        cmbProductos = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        cmbUnidadMedida = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        cmbProvedores = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();

        jtableAll.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4", "Title 5", "Title 6", "Title 7"
            }
        ));
        jtableAll.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jtableAllMouseExited(evt);
            }
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtableAllMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtableAll);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 822, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
                .addGap(86, 86, 86))
        );

        jTabbedPane3.addTab("Index", jPanel2);

        jLabel1.setText("Unidad Medida");

        jLabel2.setText("Strock Minimo");

        jLabel3.setText("Stock Maximo");

        jLabel4.setText("Precio Unitario");

        jLabel5.setText("Precio Mayoreo");

        jLabel6.setText("Cantidad");

        cmbProductos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jButton1.setText("Guardar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");

        cmbUnidadMedida.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Gramos", "Kilogramos", "Litros", "Metros", "Pieza" }));

        jLabel7.setText("Producto");

        cmbProvedores.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cmbProvedores.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbProvedoresItemStateChanged(evt);
            }
        });

        jLabel8.setText("Provedores");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator2)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(18, 18, 18)
                        .addComponent(cmbUnidadMedida, javax.swing.GroupLayout.PREFERRED_SIZE, 284, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addComponent(txtPrecioUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addComponent(txtStockMinimo, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel5)
                        .addGap(18, 18, 18)
                        .addComponent(txtPrecioMayoreo, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jSeparator1)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(txtStockMaximo, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(58, 58, 58)
                        .addComponent(jLabel6)
                        .addGap(18, 18, 18)
                        .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 40, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jButton1)
                                .addGap(100, 100, 100))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cmbProvedores, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                                .addGap(59, 59, 59)
                                .addComponent(jLabel7)
                                .addGap(18, 18, 18)
                                .addComponent(cmbProductos, javax.swing.GroupLayout.PREFERRED_SIZE, 289, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addGap(52, 52, 52)
                                .addComponent(jButton2)))))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4)
                            .addComponent(txtPrecioUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(cmbUnidadMedida, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5)
                            .addComponent(txtStockMinimo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPrecioMayoreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(9, 9, 9)
                        .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6)
                            .addComponent(txtStockMaximo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(185, 185, 185)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmbProvedores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8)
                            .addComponent(jLabel7)
                            .addComponent(cmbProductos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(38, 38, 38)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(287, Short.MAX_VALUE))
        );

        jTabbedPane3.addTab("Agregar Nuevo", jPanel3);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(46, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane3)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmbProvedoresItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbProvedoresItemStateChanged
        try {
            Item ob = (Item) cmbProvedores.getSelectedItem();
            List<Catproductos> listaAlmacen = ctrlAlmacen.<Catproductos>selectItemsbyIdProvedor(ob.getId());
            cmbProductos.removeAllItems();
            for (int i = 0; i < listaAlmacen.size(); i++) {
                cmbProductos.addItem(new Item(listaAlmacen.get(i).getId(), listaAlmacen.get(i).getValor()));
            }
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
    }//GEN-LAST:event_cmbProvedoresItemStateChanged

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        Item ob = (Item) cmbProductos.getSelectedItem();
        Catproductos ctrlCatalogos = new Catproductos();
        ctrlCatalogos.setId(ob.getId());

        Tblalmacen ctrlAlmacen = new Tblalmacen();
        ctrlAlmacen.setUnidadmedida(cmbUnidadMedida.getSelectedItem().toString());
        ctrlAlmacen.setCantidad(Integer.parseInt(txtCantidad.getText()));
        ctrlAlmacen.setPreciomayoreo(Double.parseDouble(txtPrecioMayoreo.getText()));
        ctrlAlmacen.setPrecioUnitario(Double.parseDouble(txtPrecioUnitario.getText()));
        ctrlAlmacen.setStockMaximo(Integer.parseInt(txtStockMaximo.getText()));
        ctrlAlmacen.setStockminimo(Integer.parseInt(txtStockMinimo.getText()));
        ctrlAlmacen.setCatproductos(ctrlCatalogos);

        ctrlCatalogos.getTblalmacens().add(ctrlAlmacen);

        ctrlGeneric.<Tblalmacen>createItem(ctrlAlmacen);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jtableAllMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtableAllMouseExited
//        Tblalmacen ctrlAlmacen = new Tblalmacen();
//        ctrlAlmacen.setId(Integer.parseInt(String.valueOf(jtableAll.getValueAt(jtableAll.getSelectedRow(), 0))));
//        ctrlAlmacen.setUnidadmedida(String.valueOf(jtableAll.getValueAt(jtableAll.getSelectedRow(), 7)));
//        ctrlAlmacen.setCantidad(Integer.parseInt(String.valueOf(jtableAll.getValueAt(jtableAll.getSelectedRow(), 1))));
//        ctrlAlmacen.setPreciomayoreo(Double.parseDouble(String.valueOf(jtableAll.getValueAt(jtableAll.getSelectedRow(), 4))));
//        ctrlAlmacen.setPrecioUnitario(Double.parseDouble(String.valueOf(jtableAll.getValueAt(jtableAll.getSelectedRow(), 3))));
//        ctrlAlmacen.setStockMaximo(Integer.parseInt(String.valueOf(jtableAll.getValueAt(jtableAll.getSelectedRow(), 5))));
//        ctrlAlmacen.setStockminimo(Integer.parseInt(String.valueOf(jtableAll.getValueAt(jtableAll.getSelectedRow(), 6))));
//        ctrlGeneric.<Tblalmacen>updateItem(ctrlAlmacen);
    }//GEN-LAST:event_jtableAllMouseExited

    private void jtableAllMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtableAllMouseClicked
      
    }//GEN-LAST:event_jtableAllMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cmbProductos;
    private javax.swing.JComboBox cmbProvedores;
    private javax.swing.JComboBox cmbUnidadMedida;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JTabbedPane jTabbedPane3;
    private javax.swing.JTable jtableAll;
    private javax.swing.JTextField txtCantidad;
    private javax.swing.JTextField txtPrecioMayoreo;
    private javax.swing.JTextField txtPrecioUnitario;
    private javax.swing.JTextField txtStockMaximo;
    private javax.swing.JTextField txtStockMinimo;
    // End of variables declaration//GEN-END:variables

    class Item {

        private int id;
        private String description;

        public Item(int id, String description) {
            this.id = id;
            this.description = description;
        }

        public int getId() {
            return id;
        }

        public String getDescription() {
            return description;
        }

        @Override
        public String toString() {
            return description;
        }
    }
}
